﻿using LocationTrackingService.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace LocationTrackingService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in both code and config file together.
    public class LocationTrackingService : ITrackingService
    {
         [WebInvoke(Method = "GET",
                    ResponseFormat = WebMessageFormat.Json,
                    UriTemplate = "data/{value}")]
        public string GetData(string value)
        {
            return string.Format("You entered: {0}", value);
        }
        //tracking
        [WebInvoke(Method = "POST",
                    ResponseFormat = WebMessageFormat.Json,
                    UriTemplate = "register")]
       public TrackingResponse RegisterUser(User user)
        {
            using (LocationTrackingEntities context = new LocationTrackingEntities())
            {
                TrackingResponse response = new TrackingResponse();
                //check user first
                UserEntity foundUser = null;
                try
                {
                    foundUser = context.users.Where(u => u.username == user.UserName).First();
                }
                catch (Exception)
                {
                }
                if (foundUser != null)
                {
                    response.Result = false;
                    response.Message = "User " + user.UserName + " is existed";
                    return response;
                }
                try
                {
                    context.AddTousers(ModelConverter.convertToEntity(user));
                    if (context.SaveChanges() > 0)
                    {
                        response.Result = true;
                        return response;
                    }
                }
                catch (Exception e)
                {
                    response.Result = false;
                    response.Message = e.Message;
                }
                return response;
            }
        }
         [WebInvoke(Method = "GET",
                    ResponseFormat = WebMessageFormat.Json,
                    UriTemplate = "validate/{userName}/{pin}")]
        public bool ValidateUser(string userName, string pin)
        {
            using (LocationTrackingEntities context = new LocationTrackingEntities())
            {
                try
                {
                    var result = context.users.Where(p => p.username == userName && p.pin == pin).First();
                    if (result != null)
                    {
                        return true;
                    }
                }
                catch (Exception)
                {
                    return false;
                }
                return false;
            }
        }
         [WebInvoke(Method = "GET",
                     ResponseFormat = WebMessageFormat.Json,
                     UriTemplate = "validatepassword/{userName}/{password}")]
         public bool ValidatePasswordUser(string userName, string password)
         {
             using (LocationTrackingEntities context = new LocationTrackingEntities())
             {
                 try
                 {
                     var result = context.users.Where(p => p.username == userName && p.password == password).First();
                     if (result != null)
                     {
                         return true;
                     }
                 }
                 catch (Exception)
                 {
                     return false;
                 }
                 return false;
             }
         }
         [WebInvoke(Method = "GET",
                    ResponseFormat = WebMessageFormat.Json,
                    UriTemplate = "getusers")]
        public User[] GetUsers()
        {
            using (LocationTrackingEntities context = new LocationTrackingEntities())
            {
                List<User> result = new List<User>();
                foreach (UserEntity entity in context.users)
                {
                    result.Add(ModelConverter.convertToModel(entity));
                }
                return result.ToArray();
            }
        }
         [WebInvoke(Method = "POST",
                    ResponseFormat = WebMessageFormat.Json,
                    UriTemplate = "addlocation")]
        public bool AddLocation(Location location)
        {   
            using (LocationTrackingEntities context = new LocationTrackingEntities())
            {
                bool result = false;
                try
                {
                    context.AddTolocations(ModelConverter.convertToEntity(location));
                    if (context.SaveChanges() > 0)
                        result = true;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    result = false;
                }
                return result;
            }
        }
        [WebInvoke(Method = "GET",
                    ResponseFormat = WebMessageFormat.Json,
                    UriTemplate = "getlocationhistory/{username}")]
         public Location[] GetUserLocationHistory(string username)
        {
            using (LocationTrackingEntities context = new LocationTrackingEntities())
            {
                try
                {
                    List<Location> result = new List<Location>();
                    List<LocationEntity> locations = context.locations.Where(p => p.username == username).ToList();
                    foreach (LocationEntity location in locations)
                    {
                        if (DateTime.Now.AddDays(-7) < location.time)
                            result.Add(ModelConverter.convertToModel(location));
                    }
                    return result.ToArray();
                }
                catch (Exception)
                {
                }
                return null;
            }
        }
         [WebInvoke(Method = "GET",
                    ResponseFormat = WebMessageFormat.Json,
                    UriTemplate = "getlastlocation/{username}")]
        public Location GetLastLocation(string username)
        {
            using (LocationTrackingEntities context = new LocationTrackingEntities())
            {
                try
                {
                    var location = context.locations.Where(p => p.username == username).OrderByDescending(p => p.id).First();
                    if (location != null)
                    {
                        return ModelConverter.convertToModel(location);
                    }
                }
                catch (Exception)
                {
                }
                return null;
            }
        }
    }
}
